package net.hn.hnms.biz.project.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import net.hn.hnms.biz.project.domain.LegalPerson;
import net.hn.hnms.biz.project.domain.ProjectFile;
import net.hn.hnms.biz.project.domain.ProjectInfo;
import net.hn.hnms.biz.project.mapper.ProjectFileMapper;
import net.hn.hnms.biz.project.mapper.ProjectInfoMapper;
import net.hn.hnms.biz.project.request.ProjectFileReq;
import net.hn.hnms.biz.project.request.ProjectInfoReq;
import net.hn.hnms.biz.project.service.ProjectInfoService;
import net.hn.hnms.biz.project.vo.ProjectInfoVo;
import net.hn.hnms.biz.project.vo.SupplierInfoVo;
import net.hn.hnms.sys.common.core.constant.Constants;
import net.hn.hnms.sys.common.core.domain.ResponsePage;
import net.hn.hnms.sys.common.core.exception.HuaNengException;
import net.hn.hnms.sys.common.mybatis.core.page.PageQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class ProjectInfoServiceImpl implements ProjectInfoService {

    @Autowired
    private ProjectInfoMapper projectInfoMapper;

    @Autowired
    private ProjectFileMapper projectFileMapper;

    @Override
    @Transactional
    public int insert(ProjectInfoReq projectInfoReq) {
        ProjectInfo insertObj = new ProjectInfo();
        BeanUtil.copyProperties(projectInfoReq, insertObj);
        int falg = projectInfoMapper.insert(insertObj);
        List<ProjectFileReq> projectFileList = projectInfoReq.getProjectFile();
        for(ProjectFileReq pro:projectFileList){
            pro.setProjectId(insertObj.getProjectId());
            ProjectFile file = new ProjectFile();
            BeanUtil.copyProperties(pro, file);
            projectFileMapper.insert(file);
        }
        return falg;
    }

    @Override
    public ResponsePage<ProjectInfoVo> selectPageList(ProjectInfoReq projectInfoReq, PageQuery pageQuery) {
//        Page<ProjectInfoVo> page = projectInfoMapper.selectPageList(pageQuery.build(), projectInfoReq);
        Page<ProjectInfo> page = projectInfoMapper.selectPage(pageQuery.build(), buildQuery(projectInfoReq).orderByDesc(ProjectInfo::getCreateTime));
        return new ResponsePage<>(page.getRecords(), page.getTotal());
    }

    /**
     * 构造查询条件
     * @param req
     * @return LambdaQueryWrapper<ExecutionOther> 查询条件
     */
    private LambdaQueryWrapper<ProjectInfo> buildQuery(ProjectInfoReq req){
        return new LambdaQueryWrapper<ProjectInfo>()
                .like(StrUtil.isNotBlank(req.getProjectCode()), ProjectInfo::getProjectCode, req.getProjectCode())
                .like(StrUtil.isNotBlank(req.getProjectName()), ProjectInfo::getProjectName, req.getProjectName())
                .like(StrUtil.isNotBlank(req.getProjectInitiator()), ProjectInfo::getProjectInitiator, req.getProjectInitiator())
                .like(StrUtil.isNotBlank(req.getSupervisePerson()), ProjectInfo::getSupervisePerson, req.getSupervisePerson())
                .eq(StrUtil.isNotBlank(req.getProjectType()), ProjectInfo::getProjectType, req.getProjectType())
                .eq(StrUtil.isNotBlank(req.getMineCode()), ProjectInfo::getMineCode, req.getMineCode())
                .eq(StrUtil.isNotBlank(req.getProjectState()), ProjectInfo::getProjectState, req.getProjectState())
                .ge(req.getBeginDateTime() != null, ProjectInfo::getProjectDate, req.getBeginDateTime())
                .le(req.getEndDateTime() != null, ProjectInfo::getProjectDate, req.getEndDateTime());
    }

    @Override
    @Transactional
    public void updateDeleteByIds(List<String> idList) {
        projectInfoMapper.updateDeleteByIds(idList);
        projectInfoMapper.updateDeleteBudgetByIds(idList);
    }

    @Override
    @Transactional
    public void updateById(ProjectInfoReq projectInfoReq) {
        ProjectInfo updateObj = new ProjectInfo();
        BeanUtil.copyProperties(projectInfoReq, updateObj);
        int updateCount = projectInfoMapper.updateById(updateObj);
        if (updateCount == 0) {
            throw new HuaNengException(Constants.FAIL, "数据不存在");
        }
        projectFileMapper.updateDeleteById(updateObj.getProjectId());
        List<ProjectFileReq> projectFileList = projectInfoReq.getProjectFile();
        for(ProjectFileReq pro:projectFileList){
            pro.setProjectId(updateObj.getProjectId());
            ProjectFile file = new ProjectFile();
            BeanUtil.copyProperties(pro, file);
            file.setProjectFileId(null);
            projectFileMapper.insert(file);
        }
    }

    @Override
    public ProjectInfoVo selectDetailById(String id) {
        ProjectInfo projectInfo = projectInfoMapper.selectById(id);
        if (projectInfo == null) {
            throw new HuaNengException(Constants.FAIL, "数据不存在");
        }
        ProjectInfoVo projectInfoVo = new ProjectInfoVo();
        BeanUtil.copyProperties(projectInfo, projectInfoVo);
        List<ProjectFile> projectFile = projectFileMapper.selectFileById(id);
        projectInfoVo.setProjectFile(projectFile);
        return projectInfoVo;
    }

    @Override
    public List<ProjectInfoVo> getProjectList(ProjectInfoReq projectInfoReq) {
        return projectFileMapper.getProjectList();
    }

    @Override
    public List<ProjectInfoVo> getProjectListByType(String projectType) {
        return projectFileMapper.getProjectListByType(projectType);
    }

    @Override
    public List<LegalPerson> getLegalPerson(ProjectInfoReq projectInfoReq) {
        return projectFileMapper.getLegalPerson();
    }


    @Override
    public List<SupplierInfoVo> getSupplierInfo(ProjectInfoReq projectInfoReq) {
        return projectFileMapper.getSupplierInfo();
    }

}
